<?php

namespace app\admin\controller;

use houdunwang\arr\Arr;

class Category extends Common {
    protected $db = null;

    protected function _initialize() {
        parent::_initialize();
        $this->db = new \app\common\model\Category();
    }

    // 首页
    public function index() {
        // 获取栏目数据
        $list = $this->db->select();
        $list = Arr::tree($list,'cate_name','cate_id','cate_pid');
        $this->assign('list', $list);
        return $this->fetch();
    }

    // 添加栏目
    public function store() {

        if (request()->isPost()) {
            $ret = $this->db->store(input('post.'));
            if ($ret['valid']) {
                $this->success($ret['msg'], 'index');
                exit;
            } else {
                $this->error($ret['msg'], null, '', 1);
                exit;
            }
        }
        return $this->fetch();
    }

    // 添加子级栏目
    public function addson() {
        $cate_id  = input('param.cate_id');

        if (request()->isPost()){
            $ret = $this->db->store(input('post.'));
            if ($ret['valid']) {
                $this->success($ret['msg'], 'index');
                exit;
            } else {
                $this->error($ret['msg'], null, '', 1);
                exit;
            }
        }

        $cateData = $this->db->find($cate_id);
        return $this->fetch('store', ['cateData' => $cateData]);
    }
}
